package com.jz.pay4.config;

import com.github.kristofa.brave.Brave;
import com.github.kristofa.brave.mysql.MySQLStatementInterceptorManagementBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

@Configuration
public class ApplicationContextConfig {

    @Bean
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }

    @Value("${spring.redis.host}")
    private String redisHost;
    @Value("${spring.redis.port}")
    private int redisPort;
    @Value("${spring.redis.password}")
    private String redisPassword;
    @Value("${spring.redis.timeout}")
    private int redisTimeout;


    @Bean
    public JedisPool getJedisPool(JedisPoolConfig JedisPoolConfig){

        String host = redisHost;
        int port = redisPort;
        int timeout = redisTimeout;
        String password = redisPassword;
        JedisPool jedisPool = new JedisPool(JedisPoolConfig,host,port,timeout,password);
        return jedisPool;
    }

    @ConfigurationProperties(prefix = "spring.redis.jedis")
    @Bean
    public JedisPoolConfig getJedisPoolConfig(){
        JedisPoolConfig JedisPoolConfig = new JedisPoolConfig();
        JedisPoolConfig.setTestOnBorrow(false);
        JedisPoolConfig.setTestOnReturn(false);
        JedisPoolConfig.setTestOnCreate(true);
        return JedisPoolConfig;
    }

//    @Bean
//    public RedisTemplate<String, Serializable> getRedisTemplate(){
//        return new RedisTemplate<String, Serializable>();
//    }

    @Bean
    public MySQLStatementInterceptorManagementBean mySQLStatementInterceptorManagementBean() {
        return new MySQLStatementInterceptorManagementBean(new Brave.Builder().build().clientTracer());
    }
}
